from django.http import JsonResponse


def JSONResponse(code, **message):
    if 'message' in message:
        res = {"code": code, "message": message['message']}
    elif 'token' in message:
        res = {"code": code, "data": message['token']}
    elif 'data' in message and 'total' not in message:
        res = {"code": code, "data": message['data']}
    elif 'total' in message and 'data' in message:
        res = {
            "code": code,
            "data": {
                "total": message['total'],
                "items": [t for t in message['data']]
            }
        }
    else:
        res = {"code": code}
    return JsonResponse(res)


def JSONResponseHaveMsg(code, data=None, message=None, total=None):
    if message != None:
        if data != None:
            if total != None:
                res = {
                    "code": code,
                    "data": {
                        "total": total,
                        "items": [t for t in data]
                    },
                    "message": message
                }
            else:
                res = {"code": code, "data": data, "message": message}
        else:
            res = {"code": code, "message": message}
    else:
        if data != None:
            if total != None:
                res = {
                    "code": code,
                    "data": {
                        "total": total,
                        "items": [t for t in data]
                    }
                }
            else:
                res = {"code": code, "data": data}
        else:
            res = {"code": code}

    return JsonResponse(res)
